<?php
include_once dirname(__FILE__).'/../RDBConfig.php';
include_once dirname(__FILE__).'/../../interface/AttributeGroupDAOInterface.php';
include_once dirname(__FILE__).'/../../object/AttributeGroup.php';

class AttributeGroupDAO implements AttributeGroupDAOInterface {
	
	public static $TABLE_NAME = 'ATTRIBUTE_GROUP';
	public static $COL_ATTRIBUTE_GROUP_PK  = 'ATTRIBUTE_GROUP_PK';
	public static $COL_GROUP_NAME = 'GROUP_NAME';
	public static $COL_GROUP_DESCRIPTION = 'GROUP_DESCRIPTION';
	
	public function __construct() {
	
	}
	
	/**
	 * Them mot dong vao 'ATTRIBUTE_GROUP'.
	 * Tra ve so dong da them
	 * @see AttributeGroupDAOInterface::insert()
	 * @param AttributeGroup $group
	 * @return int
	 */
	public function insert($group) {
		$table = self::$TABLE_NAME;
		$colGroupPK = self::$COL_ATTRIBUTE_GROUP_PK;
		$colGroupName = self::$COL_GROUP_NAME;
		$colGroupDes = self::$COL_GROUP_DESCRIPTION;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        INSERT INTO $table
        	($colGroupPK, $colGroupName, $colGroupDes)
                VALUES (:group_pk :group_name, :group_des)
END;
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':group_pk' => $group->getGroupPK(),
				':group_name' => $group->getGroupName(),
				':group_des'=> $group->getGroupDescription()
		);
		
		try {
			$conn->beginTransaction(); // bo che do tu dong commit
		
			$sql->execute($data); // do du lieu vao cau truy van
		
			$conn->commit(); // commit de cap nhat thay doi vao CSDL
		
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
			$conn->rollBack();
		}
		
		$conn = null; // free memory
		return $sql->rowCount(); // tra ve so dong da them
	}
	
	/**
	 * Xoa mot dong co PK tuong ung.
	 * Tra ve so dong da xoa.
	 * @see AttributeGroupDAOInterface::delete()
	 * @param AttributeGroup $group
	 * @return int
	 */
	public function delete($group) {
		$table = self::$TABLE_NAME;
		$colGroupPK = self::$COL_ATTRIBUTE_GROUP_PK;
		$colGroupName = self::$COL_GROUP_NAME;
		$colGroupDes = self::$COL_GROUP_DESCRIPTION;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        DELETE FROM $table
        	WHERE $colGroupPK = :group_pk
END;
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':group_pk' => $group->getGroupPK()			
		);
		
		try {
			$conn->beginTransaction(); // bo che do tu dong commit
		
			$sql->execute($data); // do du lieu vao cau truy van
		
			$conn->commit(); // commit de cap nhat thay doi vao CSDL
		
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
			$conn->rollBack();
		}
		
		$conn = null; // free memory
		return $sql->rowCount(); // tra ve so dong da them
	}
	/**
	 * Cap nhat mot dong co PK = $old->PK (khong cap nhat cot PK).
	 * Tra ve so dong da cap nhat.
	 * @see AttributeGroupDAOInterface::update()
	 * @param AttributeGroup $old
	 * @param AttributeGroup $new
	 * @return int
	 */
	public function update($old, $new) {
		$table = self::$TABLE_NAME;
		$colGroupPK = self::$COL_ATTRIBUTE_GROUP_PK;
		$colGroupName = self::$COL_GROUP_NAME;
		$colGroupDes = self::$COL_GROUP_DESCRIPTION;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        UPDATE $table
        	SET
        		$colGroupName = :group_name,
        		$colGroupDes = :group_des
        	WHERE $colGroupPK = :group_pk
END;
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':group_pk' => $old->getGroupPK(),
				':group_name' => $new->getGroupName(),
				':group_des' => $new->getGroupDescription()
		);
		
		try {
			$conn->beginTransaction(); // bo che do tu dong commit
		
			$sql->execute($data); // do du lieu vao cau truy van
		
			$conn->commit(); // commit de cap nhat thay doi vao CSDL
		
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
			$conn->rollBack();
		}
		
		$conn = null; // free memory
		return $sql->rowCount(); // tra ve so dong da them
	}
	
	/**
	 * Tra ve mot AttributeGroup co PK tuong ung, neu khong tra ve AttributeGroup(NULL)
	 * @see AttributeGroupDAOInterface::getAttributeGroupByPK()
	 * @param int $groupPK
	 * @return AttributeGroup
	 */
	public function getAttributeGroupByPK($groupPK) {
		$table = self::$TABLE_NAME;
		$colGroupPK = self::$COL_ATTRIBUTE_GROUP_PK;
		$colGroupName = self::$COL_GROUP_NAME;
		$colGroupDes = self::$COL_GROUP_DESCRIPTION;
		
		$statement = <<<END
        SELECT *
        FROM $table
        WHERE $colGroupPK = :group_pk
        LIMIT 1
END;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
				
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':group_pk' => $groupPK
		);
		
		try {
			$sql->execute($data); // do du lieu vao cau truy van
			$sql->setFetchMode(PDO::FETCH_ASSOC);
			if($sql->rowCount() > 0) {
				$row = $sql->fetch();
				$group = new AttributeGroup($row["$colGroupPK"],
						$row["$colGroupName"], $row["$colGroupDes"]);
				return $group;
			}
			
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
		}
		$conn = null; // free memory
		
		return new AttributeGroup(0, null, null);
	}
	
}
?>